#include "animation.hpp"
#include <iostream>

		Animation::Animation(void) : x_start(-50), y_start(-665), fps(0), length(10), framerate(0.00),
										laps(0), rate(3), mario_laps(0), mario_sel(M1)
{
	this->loadTexture(MUSH, "../Ressources/player1.png");
	this->loadTexture(TUBE, "../Ressources/tube.png");
	this->loadTexture(M1, "../Ressources/mario1.png");
	this->loadTexture(M2, "../Ressources/mario2.png");
	this->loadTexture(M3, "../Ressources/mario3.png");
	this->loadTexture(M4, "../Ressources/mario4.png");
}

		Animation::~Animation(void)
{
}

void	Animation::loadTexture(int id, char *file)
{
	textures[id].loadFromFile(file);
	sprites[id].setTexture(textures[id]);
}

void	Animation::draw(sf::RenderWindow *window, unsigned int sprite)
{
	window->draw(this->sprites[sprite]);
}

void	Animation::setOrigins(void)
{
	this->sprites[MUSH].setOrigin(this->x_start, this->y_start);
	this->sprites[TUBE].setOrigin(-48, -661);
	this->sprites[M1].setOrigin(40, -665);
	this->sprites[M2].setOrigin(40, -665);
	this->sprites[M3].setOrigin(40, -665);
	this->sprites[M4].setOrigin(40, -665);
}

void	Animation::initSelectMode(int fps)
{
	this->fps = fps;
	this->framerate = (1.00 / this->fps);
	this->total_frames = this->fps * this->length;
	this->setOrigins();
}

void	Animation::resetSprites(void)
{
	this->sprites[MUSH].setPosition(0,0);
	this->sprites[M1].setPosition(0,0);
	this->sprites[M2].setPosition(0,0);
	this->sprites[M3].setPosition(0,0);
	this->sprites[M4].setPosition(0,0);
}

void	Animation::runSelectMode(sf::RenderWindow *window)
{
	this->mushAnim(window);
	if (this->laps == this->total_frames)
	{
		this->laps = 0;
		this->resetSprites();
	}
	else
		this->laps++;
}

void	Animation::mushAnim(sf::RenderWindow *window)
{
	if (this->laps >= 0 && this->laps < 13)
		this->move(UP, MUSH);
	else if ((this->laps >= 12 && this->laps < 28)
									|| (this->laps >= 44 && this->laps < 400))
		this->move(RIGHT, MUSH);
	else if (this->laps >= 28 && this->laps < 44)
		this->move(DOWN, MUSH);
	this->draw(window, MUSH);
	this->draw(window, TUBE);
}


void	Animation::move(unsigned int move, unsigned int sprite,
															unsigned int rate)
{
	rate = (rate) ? rate : this->rate;
	switch (move)
	{
		case 0:
			this->up(this->sprites[sprite], rate);
			break;
		case 1:
			this->right(this->sprites[sprite], rate);
			break;
		case 2:
			this->down(this->sprites[sprite], rate);
			break;
		case 3:
			this->left(this->sprites[sprite], rate);
			break;
	}
}

void	Animation::up(sf::Sprite &sprite, unsigned int rate)
{
	sf::Vector2f pos = sprite.getPosition();
	sprite.setPosition(pos.x, pos.y - rate);
}

void	Animation::right(sf::Sprite &sprite, unsigned int rate)
{
	sf::Vector2f pos = sprite.getPosition();
	sprite.setPosition(pos.x + rate, pos.y);
}

void	Animation::down(sf::Sprite &sprite, unsigned int rate)
{
	sf::Vector2f pos = sprite.getPosition();
	sprite.setPosition(pos.x, pos.y + rate);
}

void	Animation::left(sf::Sprite &sprite, unsigned int rate)
{
	sf::Vector2f pos = sprite.getPosition();
	sprite.setPosition(pos.x - rate, pos.y);
}